let lastSrcs ;//上次获取到的script地址
const scriptReg = /\<script.*src=["'](?<src>[^"']+)/gm;
async function extractNewScripts(){
    const html = await fetch('./?_timestmp='+Date.now())
    .then((resp)=>resp.text());
    scriptReg.lastIndex=0;
    let result=[];
    let match;
    console.log("-----检测更新--------")
    while((match=scriptReg.exec(html))){
        result.push(match.groups.src)
    }
    console.log("-----检测更新完--------")
    return result;
}
async function needUpdate(){
    const newScripts = await extractNewScripts();
    if(!lastSrcs){
        lastSrcs = newScripts;
        return false;
    }
    let result = false;
    if(lastSrcs.length!==newScripts.length){
        result = true;
    }
    for(let i=0;i<lastSrcs.length;i++){
        if(lastSrcs[i]!==newScripts[i]){
            result=true;
            break;
        }
    }
    lastSrcs = newScripts;
    return result;
}
const DURATION = 2000;
function autoRefresh(){
    setInterval(async ()=>{
        const willUpdate =await needUpdate();
        if(willUpdate){
            const result = confirm('页面有更新，点击确定刷新页面');
            if(result){
                location.reload();
            }
        }
    },DURATION)
}